package com.sen.controller;


import com.sen.common.api.ApiResult;
import com.sen.common.jwt.JwtUtil;
import com.sen.model.dto.LoginDto;
import com.sen.model.dto.RegisterDto;
import com.sen.model.entity.BmsTip;
import com.sen.model.entity.UmsUser;
import com.sen.service.BmsTipService;
import com.sen.service.UmsUserService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ergou
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/ums/user")
public class UmsUserController {
    @Resource
    private UmsUserService umsUserService;

    /**
     * 用户注册
     * @return BmsBillboard
     */
    @PostMapping("/register")
    public ApiResult<Map<String,Object>> register(@Valid @RequestBody RegisterDto dto){
        UmsUser umsUser = umsUserService.executeRegister(dto);

        if (ObjectUtils.isEmpty(umsUser)){
            //这里应该执行不到
            return ApiResult.failed("账号注册失败");
        }
        //这里返回用户信息，暂时前端用不到
        Map<String,Object> map = new HashMap<>(16);
        map.put("user",umsUser);
        return ApiResult.success(map);
    }

    /**
     * 用户登录
     * @return BmsBillboard
     */
    @PostMapping("/login")
    public ApiResult<Map<String,Object>> login(@Valid @RequestBody LoginDto dto){
        String token = umsUserService.executeLogin(dto);

        if (ObjectUtils.isEmpty(token)){
            //这里应该执行不到
            return ApiResult.failed("账号或密码错误");
        }
        //这里返回用户信息，暂时前端用不到
        Map<String,Object> map = new HashMap<>(16);
        map.put("token",token);
        return ApiResult.success(map,"登录成功");
    }

    /**
     * 请求用户信息
     * @return UmsUser
     *
     *@RequestHeader 会判断header里面有没有USER_NAME属性，有的话传递过来
     */
    @GetMapping("/info")
    public ApiResult<UmsUser> getUser(@RequestHeader(value = JwtUtil.USER_NAME) String userName){
        UmsUser user = umsUserService.getUserByUsername(userName);
        return ApiResult.success(user);
    }

    /**
     * 注销用户信息
     * @return Object
     *
     *@RequestHeader 会判断header里面有没有USER_NAME属性，有的话传递过来
     */
    @GetMapping("/logout")
    public ApiResult<Object> logout(){
        return ApiResult.success(null,"注销成功");
    }
}
